import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
Vue.prototype.$axios = axios
Vue.prototype.$url = 'http://127.0.0.1:8081/'
// axios.defaults.baseURL = 'http://127.0.0.1:8081/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8;Access-Control-Allow-Origin=*'
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.prototype.$error = ()=>{
  this.$message.error('网络异常请稍后重试 ')
}
//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
Vue.prototype.$verifyPhone = function (phone) {
  if(!(/^1[3456789]\d{9}$/.test(phone))){
      this.$message.warning('手机号码格式有问题')
      return false
  }
  return true
}
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  // if(!user ){
  //   next({path:'/login'})
  // }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if( !user && to.path == '/register'){
    next({ redirect: '/register' })
  }else if ( !user  && to.path != '/login') {
    next({ path: '/login' })
  } else{
    next()
  }
})
axios.interceptors.request.use(config=>{
  if(/(\/login)|(\/register)/.test(config.url)){
    return config
  }
  let token = localStorage.getItem("token")
  config.headers["token"]=token
  return config
},error => {
  this.$message.error("网络错误")
})
axios.interceptors.response.use(res=>{
  if(res.status===407){
    this.$message.error("登录状态已过期")
  }
  let token = res.data.tk
  if(token){
    localStorage.setItem("token",token)
  }
  return res
},error => {
  this.$message.error("网络错误")
})
//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

